/**
 * Created by wsyao
 * 全局工具类
 *
 /**
 * 判断是否为空
 * @param str
 * @returns {boolean}
 *
 */
const isEmpty = str => {
  if (typeof str === 'string') {
    str = str.trim()
  }
  if (str == null || str === '' || str === undefined || str === 'null' || str === 'undefined') {
    return true
  }
  return false
}

/**
 * 时间戳转年月日
 * @param str
 * @returns {String}
 *
 */
const timestampToTime = str => {
  let date = new Date(str * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  let D = (date.getDate() < 10) ? ('0' + date.getDate() + ' ') : date.getDate() + ' '
  let h = (date.getHours() < 10) ? ('0' + date.getHours() + ':') : date.getHours() + ':'
  let m = (date.getMinutes() < 10) ? ('0' + date.getMinutes() + ':') : date.getMinutes() + ':'
  let s = (date.getSeconds() < 10) ? ('0' + date.getSeconds()) : date.getSeconds()
  return Y + M + D + h + m + s
}


/*16进制转10进制*/
const ex16hex = value => {
  value = stripscript(value);
  value = value.replace("0x", "");
  var arr = value.split("");
  arr = arr.reverse();
  var len = arr.length;
  var res = 0;
  $.each(arr, function(i, v) {
    var num = hex_change(v);
    console.log(num)
    res += muti16(num, i);
  });
  return res;
}

// 字符转16进制数字
const hex_change = v => {
  var res;
  switch (v) {
    case "a":
      res = 10;
      break;
    case "b":
      res = 11;
      break;
    case "c":
      res = 12;
      break;
    case "d":
      res = 13;
      break;
    case "e":
      res = 14;
      break;
    case "f":
      res = 15;
      break;
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
      res = Number(v);
      break;
    default:
      res = 0;
      break;
  }
  return res;
}

/*
过滤所有特殊字符
*/
const stripscript = s => {
  var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？↵\r\n]");
  var rs = "";
  for (var i = 0; i < s.length; i++) {
    rs = rs + s.substr(i, 1).replace(pattern, '');
  }
  return rs;
}

// 返回 v 乘以 n 个 16 的积
const muti16 = function(v, n) {
  var temp = v;
  for (var i = 0; i < n; i++) {
    temp *= 16;
  }
  return temp;
}

window.Tools = {
  // 公共方法
  isEmpty, //判断是否为空
  timestampToTime, //时间戳转换
  ex16hex //16进制转10进制
}
